//
//  BKJianJieViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/2.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKJianJieViewController.h"
#import "BKColleageViewModel.h"
#import <UIImageView+AFNetworking.h>
#import "BKXiaoYouTableViewCell.h"
#import "BKQiTaTableViewCell.h"
#import <MBProgressHUD.h>

static NSString *xiaoyou = @"xiaoyou";
static NSString *qita = @"qita";
@interface BKJianJieViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *xiaoyouTableView;
@property (weak, nonatomic) IBOutlet UITableView *qitaTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jianjieHeight;
@property (nonatomic ,copy) NSDictionary *dataDic;
@property (weak, nonatomic) IBOutlet UITextView *jianjieLabel;
@property (weak, nonatomic) IBOutlet UIView *jianjieView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (nonatomic ,strong) MBProgressHUD *hud ;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xiaoyouHeight;

@end

@implementation BKJianJieViewController
- (IBAction)p_bakc:(id)sender {
    [_delegate dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_xiaoyouTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKXiaoYouTableViewCell class]) bundle:nil]  forCellReuseIdentifier:xiaoyou];
    [_qitaTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKQiTaTableViewCell class]) bundle:nil] forCellReuseIdentifier:qita];
    
    _qitaTableView.separatorColor = [UIColor clearColor];
    
    _xiaoyouTableView.layer.cornerRadius =3 ;
    _xiaoyouTableView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _xiaoyouTableView.layer.borderWidth = 1.0;
    
    _qitaTableView.layer.cornerRadius =3 ;
    _qitaTableView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _qitaTableView.layer.borderWidth = 1.0;
    _jianjieView.layer.cornerRadius = 3;
    _jianjieView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _jianjieView.layer.borderWidth= 1.0;
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.headIndent = 30;
    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.labelText = @"正在加载学校信息";
    [self.view addSubview:_hud];
    [_hud show:YES];
   
    // Do any additional setup after loading the view from its nib.
    
    __weak BKJianJieViewController *weakSelf = self;
    [BKColleageViewModel jianjieWithCid:_cid success:^(NSDictionary *dic) {
    
        [weakSelf.hud hide:YES];
        weakSelf.dataDic = dic;
    } failure:^(NSString *message) {
        [weakSelf.hud hide:YES];
    }];
    _nameLabel.text = _name;
    
    _xiaoyouTableView.bounds = CGRectMake(0, 0, _xiaoyouTableView.bounds.size.width, 120 * [_dataDic[@"xiaoyou"] count]);
    
    
}


- (void)setName:(NSString *)name{
    _name = name;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setDataDic:(NSDictionary *)dataDic{

    if (dataDic != nil) {
        _dataDic = dataDic;
        
        NSString *jianjie = _dataDic[@"intr"];
        
        UIFont *font = [UIFont systemFontOfSize:12];
        _jianjieLabel.font = font;
        CGSize size = CGSizeMake(_jianjieLabel.frame.size.width , MAXFLOAT);
        CGSize labelsize = [jianjie sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
        
        _jianjieLabel.text = jianjie;
        if (labelsize.height >= 1000) {
            _jianjieHeight.constant = 1000;
        } else {
            _jianjieHeight.constant = labelsize.height + 250;
        }
        
        CGFloat height = [_dataDic[@"xiaoyou"] isKindOfClass:[NSArray class]]?[_dataDic[@"xiaoyou"] count]:0;
        height += 70;
        if (height > 300) {
            _xiaoyouHeight.constant = 300;
        } else {
            _xiaoyouHeight.constant = height;
        }
           
        
        [_imageView setImageWithURL:[NSURL URLWithString:_dataDic[@"img"]]];
        _nameLabel.text = _dataDic[@"name"];
        _titleLabel.text = _dataDic[@"name"];
        [self.view layoutIfNeeded];
        [_xiaoyouTableView reloadData];
        [_qitaTableView reloadData];
        
        
        
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ([tableView isEqual:_xiaoyouTableView]) {
        return [_dataDic[@"xiaoyou"] isKindOfClass:[NSArray class]]?[_dataDic[@"xiaoyou"] count]:0;
    }
    return 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isEqual:_xiaoyouTableView]) {
        NSDictionary *xiaoyouDict = _dataDic[@"xiaoyou"][indexPath.row];
        BKXiaoYouTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:xiaoyou];
        cell.nameLabel.text = xiaoyouDict[@"name"];
        [cell.photoView setImageWithURL:[NSURL URLWithString:xiaoyouDict[@"portrait"]]];
        cell.infoLabel.text = xiaoyouDict[@"intr"];
        return cell;
        
    }
    if (![tableView isEqual:_qitaTableView]) {
        return nil;
    }
    BKQiTaTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:qita];
    switch (indexPath.row) {
        case 0:
            cell.ca1.text = @"类型";
            cell.ca2.text = _dataDic[@"type"];
            break;
        case 1:
            cell.ca1.text = @"所在省份";
            cell.ca2.text = _dataDic[@"location"];
            break;
        case 2:
            cell.ca1.text = @"城市";
            cell.ca2.text = _dataDic[@"city"];
            break;
        case 3:
            cell.ca1.text = @"是否211";
            cell.ca2.text = [_dataDic[@"is211"] isEqualToString:@"0"]?@"否":@"是";
            break;
        case 4:
            cell.ca1.text = @"是否985";
            cell.ca2.text = [_dataDic[@"is985"] isEqualToString:@"0"]?@"否":@"是";
            break;
        case 5:
            cell.ca1.text = @"院校隶属";
            cell.ca2.text = _dataDic[@"lishu"] ;
            break;

        case 6:
            cell.ca1.text = @"设计研究生院";
            cell.ca2.text = [_dataDic[@"yjs"] isEqualToString:@"0"]?@"否":@"是";
            break;
        case 7:
            cell.ca1.text = @"独立学院";
            cell.ca2.text = [_dataDic[@"dl"] isEqualToString:@"0"]?@"否":@"是";
            break;

        case 8:
            cell.ca1.text = @"办学层次";
            cell.ca2.text = [_dataDic[@"level"] isEqualToString:@"1"]?@"本科":@"专科";
            break;
        case 9:
            cell.ca1.text = @"办学";
            cell.ca2.text = [_dataDic[@"banxue"] isEqualToString:@"1"]?@"公办":([_dataDic[@"banxue"] isEqualToString:@"2"]?@"民办":@"中外合办");
            break;
        case 10:
            cell.ca1.text = @"重点学科";
            cell.ca2.text = _dataDic[@"zdxk"];
            break;
        case 11:
            cell.ca1.text = @"硕士点";
            cell.ca2.text = _dataDic[@"ssd"];
            break;
        case 12:
            cell.ca1.text = @"博士点";
            cell.ca2.text = _dataDic[@"bsd"];
            break;
        case 13:
            cell.ca1.text = @"博士后流动站";
            cell.ca2.text = _dataDic[@"bsh"];
            break;
        case 14:
            cell.ca1.text = @"两院院士";
            cell.ca2.text = _dataDic[@"lyys"];
            break;
        case 15:
            cell.ca1.text = @"藏书";
            cell.ca2.text = _dataDic[@"cs"];
            break;
        case 16:
            cell.ca1.text = @"占地面积";
            cell.ca2.text = _dataDic[@"zdmj"];
            break;
        case 17:
            cell.ca1.text = @"电话";
            cell.ca2.text = _dataDic[@"landline"];
            break;
        case 18:
            cell.ca1.text = @"男女比例";
            cell.ca2.text = _dataDic[@"fm"];
            break;
         
        case 19:
            cell.ca1.text = @"在校生信息";
            NSString *stuinfo = _dataDic[@"stuinfo"];
            stuinfo = [stuinfo stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
            stuinfo = [stuinfo stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
            cell.ca2.text = stuinfo;
            break;
    }
    
    if (indexPath.row % 2 == 1) {
        cell.backgroundColor = [UIColor colorWithRed:0.910 green:0.965 blue:0.980 alpha:1.000];
    }else{
    
         cell.backgroundColor = [UIColor clearColor];
    }
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view;
    if ([tableView isEqual:_xiaoyouTableView]) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 34)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width-5, 24)];
        label.text = @"知名校友(排名无先后)";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor blackColor];
        label.numberOfLines = 0;
        view.backgroundColor = [UIColor colorWithWhite:0.969 alpha:1.000];
        [view addSubview:label];
        
        return view;
    }else{
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 34)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width-5, 24)];
        label.text = @"详细介绍";
        view.backgroundColor = [UIColor colorWithWhite:0.969 alpha:1.000];

        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor blackColor];
        [view addSubview:label];
        
        
        return view;
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 2;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 34;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isEqual:_xiaoyouTableView]) {
        return 120;
    } else {
        
        if (indexPath.row == 19) {
            NSString *stuinfo = _dataDic[@"stuinfo"];
            UIFont *font = [UIFont systemFontOfSize:13];
            stuinfo = [stuinfo stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
            stuinfo = [stuinfo stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
            CGSize size = CGSizeMake(200,20000);
            CGSize labelsize = [stuinfo sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
            return labelsize.height + 10;
        }
        return 34;
    }
    return 44;
}

@end
